#ifndef __MOTIONSEEKERNODE_H
#define __MOTIONSEEKERNODE_H

#include <string>
#include <vector>
#include <list>
#include <utility>

#include <pthread.h>
#include <semaphore.h>

#include <windows.h>



using namespace std;

#include "../GV/SceneNode.h"

#include "escapi.h"
#include "opticalFlow.h"
#include "convolution.h"

class CMotionSeekerNode : public GSceneNode
{
public:

	CMotionSeekerNode (string name);// : GSceneNode ();
	void refreshTexture ();

	void initInternal (bool creating);
	void fromStringExternal (string stream);
	string toStringExternal ();
	void StaticCreation();
	void DynamicDisplay();

	string execCommandExt (scriptPackage *pkg);
	string ExecuteCommandExternal (string command);

	void mouseEventExternal (mouseEvt* evt);

	void mouseDisableExternal ();
	 void updateAABB ();

	 void print (int i);

	 int rgbaToInt (unsigned char r,unsigned char g,unsigned char b,unsigned char a);



	 void getHistograma ();

	void trackingMovement ();

	void adjustSquare (vec3* plane, vec3* square);

	

	

	bool initSerialInterface ();
	
	bool sendSerialMsg (char* message,int size);

	void moveCameraRight ();

	void moveCameraLeft ();

	void moveCameraRightFaster ();

	void moveCameraLeftFaster ();

	void setMoveSpeedControl (int vel);

	int getMoveSpeedControl ();

	void controlCameraMovement ();

	void moveCamera ();

	void moveCamera2 ();
	void moveCamera3 ();

	void setVelType (int velType);

	void drawHistograma (float initX, float initY, float sizeX, float sizeY, int* histograma, int histogramSize);
	
	float analyseHistograma (int *oldHistograma, int* newHistograma, int sizeHistograma);	

	void matchImages (); 

	void setMovingDirect (int value)
	{
		movingDirec=value;
	}

private:

	int movingDirec;
	int limite;

	vec3 plane[4];
	vec3 planeFilter[4];
	vec3 planeGrey[4];
	vec3 planeGreyFilter[4];

	vec3 motionSquare[4];

	int* oldScreen;
	int* filter;
	int* filterGrey;
	int* greyScaleScreen;

	unsigned char* border;
	unsigned char* oldBorder;


	unsigned char* GreyScale;
	unsigned char* OldGreyScale;
	unsigned char* filterGreyScale;
	
	int collunmShift;
	

	bool m_visible;
GLuint texname;
GLuint texnameFilter;

GLuint texnameFilterGrey;




	struct SimpleCapParams capture;



	int histograma[255];
	int histogramaFiltro[255];
	int oldHistograma[255];






	bool m_serialAvailable;

	HANDLE   m_hCom; //serial handle

	int m_motionControl;

	int m_trackingControl;

	int m_velControl;

	bool m_movingCamera;

	int m_oldCenter;

	int m_velType;

	int device;

	opticalFlow * optFlow;

	

	

};


#endif
